import {
	reqCommon,
	deepMerge
} from "@/store/store_utils/reqCommon.js"
import request from "@/store/store_utils/request.js"
import utils from "@/utils/utils.js"
let {
	decimalToBase62,
	base62ToDecimal,
	dateFormat,
	getDistance,
	_toRadians,
	getQuarterMonths,
} = utils;
export default {
	callActionflowOffset: async (context, payload = {}) => {
		return reqCommon(`callActionflowOffset${"-"+payload?.response_key??""}`, context, payload, async () => {
			let {
				response_key = "response",
					actionflow_name = "",
					payload: payload_data = {},
					isrefresh = true, //为true重置vuex
					isOffset = true,
					isloading = true,
					isSave = true,
					isother = false,
					key = [],
			} = payload
			payload.response_key = response_key
			let todoRes = context.dispatch("app/runActionflowmain", {
				...{
					isdeal: payload?.isdeal,
					isLog: payload?.isLog,
					isloading: payload?.isloading,
					isthrow: payload?.isthrow,
					istoast: payload?.istoast,
					limit_time: payload?.limit_time,
					loadingTitle: payload?.loadingTitle
				},
				args: {
					actionflow_name,
					payload: payload_data
				}
			}, {
				root: true
			})
			if (!payload.isdeal) return todoRes
			return todoRes.then(result => {
				let resObjectArr = result?.status == "成功" ? result?.data ?? {} : result;
				let {
					count = 0,
						...data
				} = resObjectArr;
				let res_data = {
					model: response_key,
					raw_data: result,
					isrefresh,
					count,
					data: {},
					data_list: {}
				}
				key = Array.isArray(key) ? key : [key];
				if (key.length > 1) {
					key.forEach(keys => {
						res_data.data[keys] = data?.[keys]?.[0] ?? {};
						res_data.data_list[keys] = [
							...isOffset ? (context.state[response_key]?.data_list?.[
								keys
							] ?? []) : [],
							...data?.[keys] ?? []
						];
						delete data[keys];
					})
					if (isother) {
						res_data.data_other = data;
					}
				} else if (key.length === 1) {
					let item = data?.[key[0]];
					res_data.data = item?.[0] ?? {};
					res_data.data_list = [
						...isOffset ? (context.state[response_key]?.data_list ?? []) : [],
						...item ?? []
					]
					if (isother) {
						delete data[key[0]];
						res_data.data_other = data;
					}
				} else {
					if (Array.isArray(resObjectArr)) {
						res_data.data = resObjectArr?.[0]
						res_data.data_list = [
							...isOffset ? (context.state[response_key]?.data_list ?? []) :
							[],
							...resObjectArr
						];
					} else {
						res_data.data = resObjectArr;
					}
				}
				if (isSave) context.commit("setModelDataOffset", res_data)
				return res_data
			})
		})
	},
	batch_query: async (context, payload = {}) => {
		return reqCommon("batch_query", context, payload, async () => {
			let {
				url,
				authorization,
				list = [{
					response_key,
					isModel: true, // 标准的数据表结构数据
					model: "",
					ismergeQuery: true, //是否合并条件
					isrefresh: true, //为true重置vuex,否则将新数据合并覆盖
					where: {},
					order_by: {},
					distinct_on: "",
					offset: false,
					limit: false,
					id,
					paginator,
					fz_body: {},
					params: {},
					field_string: ""
				}]
			} = payload
			let newList = [];
			let query_list = list.reduce((newArr, item) => {
				let {
					response_key,
					isModel = true, // 标准的数据表结构数据
					model = "",
					ismergeQuery = true, //是否合并条件
					isrefresh = true, //为true重置vuex,否则将新数据合并覆盖
					where = {},
					order_by = {},
					distinct_on = "",
					offset = false,
					limit = false,
					id,
					paginator,
					fz_body = {},
					params = {},
					field_string = ""
				} = item
				payload.ismergeQuery = ismergeQuery
				payload.isrefresh = isrefresh
				if(!response_key){
					response_key = payload?.response_key || model
				}
				let query_config = context.state[model]?.query_config || {};
				field_string = field_string || payload?.field_string || context.state[model]
					?.field_string || "";
				if (isModel && ismergeQuery) {
					where = deepMerge(where, query_config?.where || {})
					order_by = deepMerge(order_by, query_config?.order_by || {})
					distinct_on = distinct_on || query_config?.distinct_on || "";
					if (offset === false) {
						offset = query_config?.offset || 0;
					}
					if (limit === false) {
						limit = query_config?.limit || 100;
					}
				}
				newList.push({
					response_key,
					isModel,
					isrefresh,
					ismergeQuery
				})
				newArr.push({
					response_key,
					field_string,
					model,
					where,
					order_by,
					distinct_on,
					offset,
					limit,
					id,
					paginator,
					fz_body,
					params
				})
				if (isModel) {
					newArr.push({
						response_key: `${response_key}_aggregate`,
						model: model + "_aggregate",
						field_string: `aggregate{count}`,
						where,
						//order_by,
						distinct_on,
						limit: 0
					})
				}
				return newArr
			}, [])
			// console.log(query_list);
			let todoRes = request.batch_query(query_list, url, authorization)
			if (!payload.isdeal) return todoRes
			return todoRes.then(res => {
				let dataList = [];
				newList.forEach(item => {
					let {
						response_key,
						isModel,
						isrefresh,
						ismergeQuery
					} = item
					let response = res?.[response_key]
					let res_data = {
						model: response_key,
						raw_data: response,
					}
					// 如果是标准模型则会存在aggregate
					let count = res?.[`${response_key}_aggregate`]?.aggregate?.count ??
						0
					if (count) {
						res_data = {
							...res_data,
							...{
								data: response?.[0],
								data_list: response,
								count,
								isrefresh,
								ismergeQuery
							}
						}
					} else {
						if (Array.isArray(response)) {
							res_data.data = response?.[0]
							res_data.data_list = response
							res_data.isrefresh = isrefresh
						} else {
							res_data.data = response
							res_data.isrefresh = isrefresh
						}
					}
					dataList.push(res_data);
					context.commit("setModelData", res_data)
				})
				return dataList
			})
		})
	},
}